package com.zheng.shop.rpc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.shop.common.exception.ShopException;
import com.zheng.shop.dao.mapper.ShopDictMapper;
import com.zheng.shop.dao.model.ShopDict;
import com.zheng.shop.rpc.api.ShopDictService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2017-12-29
 */
@Service
@Transactional(rollbackFor = ShopException.class)
@BaseService
public class ShopDictServiceImpl extends BaseServiceImpl<ShopDictMapper, ShopDict> implements ShopDictService {

    @Override
    public ShopDict getDictByValue(String value, String key) {
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("value='" + value + "'").and("`key`='" + key + "'");
        return selectOne(dictEntityWrapper);
    }

    @Override
    public ShopDict getDictByName(String name, String key) {
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("name='" + name + "'").where("`key`='" + key + "'");
        return selectOne(dictEntityWrapper);
    }

    @Override
    public String getDictName(String value, String key, String defaultValue) {
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("value='" + value + "'").where("`key`='" + key + "'");
        ShopDict shopDict = selectOne(dictEntityWrapper);
        if (null != shopDict && StringUtils.isNotEmpty(shopDict.getName())) {
            return shopDict.getName();
        }
        dictEntityWrapper = new EntityWrapper();
        dictEntityWrapper.where("value='" + defaultValue + "'").where("`key`='" + key + "'");
        shopDict = selectOne(dictEntityWrapper);
        if (null != shopDict) {
            return shopDict.getName();
        }
        return null;
    }

    @Override
    public List<String> getDictNames(String[] values, String key) {
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.and("`key`=" + key + "'");
        dictEntityWrapper.in("value", values);
        List<ShopDict> shopDictList = selectList(dictEntityWrapper);
        List<String> nameList = new ArrayList<>();
        for (ShopDict shopDict : shopDictList) {
            nameList.add(shopDict.getName());
        }
        return nameList;
    }

    @Override
    public String getDictValue(String name, String key, String defaultName) {
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("name='" + name + "'").where("`key`='" + key + "'");
        ShopDict shopDict = selectOne(dictEntityWrapper);
        if (null != shopDict && StringUtils.isNotEmpty(shopDict.getValue())) {
            return shopDict.getValue();
        }
        dictEntityWrapper = new EntityWrapper();
        dictEntityWrapper.where("name='" + defaultName + "'").where("`key`='" + key + "'");
        shopDict = selectOne(dictEntityWrapper);
        if (null != shopDict && StringUtils.isNotEmpty(shopDict.getValue())) {
            return shopDict.getValue();
        }
        return null;
    }

    @Override
    public List<ShopDict> getDictList(String key) {
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("`key`='" + key + "'");
        return selectList(dictEntityWrapper);
    }

    @Override
    public int selectMaxSort() {
        return super.baseMapper.selectMaxSort();
    }

    @Override
    public boolean createDict(String dicts) throws ShopException {
        int sort = selectMaxSort() + 1;
        List<ShopDict> shopDictList = JSON.parseArray(dicts, ShopDict.class);
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();
        dictEntityWrapper.where("`key`='" + shopDictList.get(0).getKey() + "'");

        if (selectOne(dictEntityWrapper) != null) {
            throw new ShopException("添加失败,已有相同key为" + shopDictList.get(0).getKey() + "的数据字典");
        }
        for (ShopDict shopDict : shopDictList) {
            shopDict.setSort(sort);
            dictEntityWrapper = new EntityWrapper();
            dictEntityWrapper.where("`key`='" + shopDict.getKey() + "'").where("value='" + shopDict.getValue() + "'");
            if (selectOne(dictEntityWrapper) != null) {
                throw new ShopException("添加失败,已添加相同value为" + shopDict.getValue() + "的字段");
            }
            if (!insert(shopDict)) {
                throw new ShopException("添加失败");
            }
        }
        return true;
    }

    @Override
    public boolean updateDict(List<ShopDict> shopDictList, String oldKey) throws ShopException {
        List<ShopDict> dictList = getDictList(oldKey);
        Integer sort = dictList.get(0).getSort();
        String newKey = dictList.get(0).getKey();
        EntityWrapper<ShopDict> dictEntityWrapper = new EntityWrapper<>();

        if (!newKey.equals(oldKey)) {
            dictEntityWrapper.where("`key`='" + newKey + "'");
            if (selectOne(dictEntityWrapper) != null) {
                throw new ShopException("修改失败,已有key为" + newKey + "的数据字典");
            }
        }
        for (ShopDict dict : dictList) {
            if (!deleteById((dict.getId()))) {
                throw new ShopException("删除旧的数据字典失败！");
            }
        }

        for (ShopDict shopDict : shopDictList) {
            shopDict.setSort(sort);
            dictEntityWrapper = new EntityWrapper<>();
            dictEntityWrapper.where("`key`='" + shopDict.getKey() + "'").where("value='" + shopDict.getValue() + "'");
            if (selectOne(dictEntityWrapper) != null) {
                throw new ShopException("添加失败,已添加相同value为" + shopDict.getValue() + "的字段");
            }
            if (!insert(shopDict)) {
                throw new ShopException("修改失败");
            }
        }
        return true;
    }

    @Override
    public int selectDictCount(String search) {
        return super.baseMapper.selectDictCount(search);
    }

    @Override
    public boolean deleteBatchDict(List<Integer> ids) throws ShopException {
        String key;
        EntityWrapper<ShopDict> dictEntityWrapper;
        for (Integer id : ids) {
            key = selectById(id).getKey();
            dictEntityWrapper = new EntityWrapper<>();
            dictEntityWrapper.where("`key`='" + key + "'");
            if (!delete(dictEntityWrapper)) {
                throw new ShopException("删除失败");
            }
        }
        return true;
    }
}
